var http = require("http");
var qs = require("querystring");
var items = [];
var server = http.createServer(function(req,res){
    if("/" == req.url){
        switch (req.method){
            case "GET":
                show(res);
                break;
            case "POST":
                add(req,res);
                break;
            default :
                badRequest(res);
        }
    }else {
        notFound(res);
    }
});
server.listen(8899);

function show(res){
    var html = "<html><head></head><body><h1>TODO List</h1>" +
                "<ul>"+
                 items.map(function(item){
                     return "<li>" + item + "</li>";
                 }).join("")+
                "</ul>"+
                '<form action="/" method="post"><p><input name="item"><button type="submit">Add Item</button></p></form>' +
                "</body></html>";
    res.setHeader("Content-Type","text/html");
    res.setHeader("Content-Length",Buffer.byteLength(html));
    res.end(html);
}

function add(req,res){
    var body = "";
    req.setEncoding("utf-8");
    req.on("data",function(trunk){
        body += trunk;
    });
    req.on("end",function(){
        var obj = qs.parse(body);
        items.push(obj.item);
        show(res);
    });
}

function notFound(res){
    res.statusCode = 404;
    res.setHeader("Content-Type","text/plain");
    res.end("Not Found!");
}

function badRequest(res){
    res.statusCode = 400;
    res.setHeader("Content-Type","text/plain");
    res.end("Bad Request!");
}